using System;
using System.IO;
using System.Text;
using System.Reflection;

namespace InfoJetSoft.Service.Util
{
	/// <summary>
	/// Summary description for FileLog.
	/// </summary>
	public class FileLog
	{
		public static void log( string message ){
			try{
                //InfoJetSoft.Service.LogFile-->InfoBus.LogFile Modify by Sunny 2013.6.26
                //string logFile = ConfigUtil.GetSetting( "InfoJetSoft.Service.LogFile" );
                string logFile = ConfigUtil.GetSetting("InfoBus.LogFile");
				if( logFile != null ){
					FileStream stream = new FileStream( logFile, FileMode.Append );
					byte[] messageContent = System.Text.Encoding.UTF8.GetBytes( DateTime.Now.ToString() + " " + message + "\r\n" );
					stream.Write( messageContent, 0, messageContent.Length );
					stream.Close();
				}
			}catch( Exception e ){}
		}

		public static void log( Exception exception )
		{
            //InfoJetSoft.Service.LogFile-->InfoBus.LogFile Modify by Sunny 2013.6.26
            //string logFile = ConfigUtil.GetSetting( "InfoJetSoft.Service.LogFile" );
            string logFile = ConfigUtil.GetSetting("InfoBus.LogFile");
			if( logFile != null )
			{
				log( "Exception:" );
				log( exception.Message );
				log( exception.StackTrace );
				if( exception.InnerException != null )
				{
					log( "Exception.InnerException:" );
					log( exception.InnerException.Message );
					log( exception.InnerException.StackTrace );
					if( exception.InnerException.InnerException != null )
					{
						log( "Exception.InnerException.InnerException:" );
						log( exception.InnerException.InnerException.Message );
						log( exception.InnerException.InnerException.StackTrace );
					}
				}
			}
		}

		public static void logEvironment()
		{
            //InfoJetSoft.Service.LogFile-->InfoBus.LogFile Modify by Sunny 2013.6.26
			//string logFile = ConfigUtil.GetSetting( "InfoJetSoft.Service.LogFile" );
            string logFile = ConfigUtil.GetSetting("InfoBus.LogFile");
			if( logFile != null )
			{
				try
				{
					StringBuilder buffer = new StringBuilder();
					buffer.Append( "\r\n" );
					buffer.Append( "OS: " + System.Environment.OSVersion + "\r\n" );
					buffer.Append( "DotNet: " + System.Environment.Version + "\r\n" );
					buffer.Append( "\r\n" );
					foreach( Assembly assembly in AppDomain.CurrentDomain.GetAssemblies() )
					{
						buffer.Append( assembly.FullName + "\r\n" );
						buffer.Append( "\tIn GAC: " + assembly.GlobalAssemblyCache + "\r\n" );
						buffer.Append( "\tLocation: " + assembly.Location + "\r\n" );
						buffer.Append( "\tImageRuntimeVersion: " + assembly.ImageRuntimeVersion + "\r\n" );
					}
					log( buffer.ToString() );
				}
				catch( Exception e )
				{
					log( e.Message );
					log( e.StackTrace );
				}
			}
		}

		public static void log( string name, string message ){
			try
			{
				//CID 0271 //2009.04.08 //Begin
				string logFolder = ConfigUtil.GetSetting( "InfoJetSoft.Service.LogFolder" );
				//CID 0271 //2009.04.08 //End
				if( logFolder != null ){
					FileStream stream = new FileStream( logFolder + "\\" + name, FileMode.Create );
					byte[] messageContent = System.Text.Encoding.UTF8.GetBytes( message );
					stream.Write( messageContent, 0, messageContent.Length );
					stream.Close();
				}
			}
			catch( Exception e ){}
		}

		//CID 0261 //2009.03.25 //Begin
		public static void logDataObject( string dataObjectName, string xml )
		{
			try
			{
				string logFolder = ConfigUtil.GetSetting( "InfoJetSoft.Service.DataObjectLogFolder" );
				if( logFolder != null )
				{
					FileStream stream = new FileStream( logFolder + "\\" + dataObjectName + ".xml", FileMode.Create );
					byte[] messageContent = System.Text.Encoding.UTF8.GetBytes( xml );
					stream.Write( messageContent, 0, messageContent.Length );
					stream.Close();
				}
			}
			catch( Exception e ){}
		}
		//CID 0261 //2009.03.25 //End
	}
}
